package it.avantsoft.ls.base;

import org.apache.log4j.Logger;
import it.avantsoft.ls.peer.PeerHandler;
import it.avantsoft.ls.ui.GuiMenu;
import it.avantsoft.ls.ui.OutputForm;

import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class ApplicationTest {
    private final int PEERS_NUMBER = 21;

    private static Logger log = Logger.getLogger(ApplicationTest.class);

    private Application server;
    private List<Application> applicationList;

    public ApplicationTest() throws InterruptedException {
        server = new Application(true, 0);
        log.info("Server started: " + server.getGroupLeaderHandler());
        Thread.sleep(2000);

        applicationList = new ArrayList<Application>();
        for (int i = 0; i < PEERS_NUMBER; i++) {
            log.info("Start Client: " + (i + 1));
            Application application = new Application(false, (i + 1));
            applicationList.add(application);

            Thread.sleep(3000);

            PeerHandler peerHandler = application.getPeerHandler();
            //form.addOutputString(peerHandler.getNumber() + " - " + peerHandler.getPeerId());
            String peerId = peerHandler.getPeerId();
            if (peerId != null) {
                form.addSimpleString(String.valueOf(i + 1));
                form.addOutputString(peerId);
            }
        }
    }

    private Application _getServer() {
        return server;
    }

    public List<Application> _getApplicationList() {
        return applicationList;
    }

    /////////////////////////// Static

    private static ApplicationTest applicationTest;
    private static String phrase;
    public static OutputForm form;

    private static void readIn() throws Exception {
        InputStreamReader reader = new InputStreamReader(System.in);
        while (true) {
            if (reader.ready()) {

                int keyPressed = reader.read();
                parseInputKey(keyPressed);
                if (keyPressed == 10) {
                    executeInput(form);
                }

            }
            Thread.sleep(500);
        }
    }

    private static void executeInput(OutputForm form) {
        if (!form.isVisible())
            form.setVisible(true);
    }

    private static void parseInputKey(int keyPressed) {
        if (phrase == null || phrase.indexOf(10) != -1) {
            System.out.println("Reinitializing");
            phrase = "";
        }
        phrase += (char) keyPressed;
    }

    private static void initUI() {
        form = new OutputForm();
        form.setJMenuBar(new GuiMenu());
        new Thread(form).start();
    }

    ////////////////// MAIN

    public static void main(String[] args) {
        try {
            initUI();
            applicationTest = new ApplicationTest();
            readIn();
        } catch (Exception e) {
            log.error(e);  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    public static void kill(int peer) {
        Application application = applicationTest._getApplicationList().get(peer - 1);
        form.addSimpleString("INFO : Killing Peer " + application.getPeerHandler().getPeerId());
        application.kill();


    }

    public static Application getServer() {
        if (applicationTest != null)
            return applicationTest._getServer();
        return null;
    }

    public static List<Application> getApplications() {
        if (applicationTest != null)
            return applicationTest._getApplicationList();
        return null;
    }

    public static boolean peerConnected(int peerNumber) {
        Application application = applicationTest._getApplicationList().get(peerNumber - 1);

        return application.getPeerHandler().isConnected();
    }
}
